package com.ruoyi.place.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.service.TcCommonService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.place.domain.PlaceEzd;
import com.ruoyi.place.domain.bo.PlaceEzdBo;
import com.ruoyi.place.domain.vo.PlaceEzdVo;
import com.ruoyi.place.mapper.PlaceEzdMapper;
import com.ruoyi.place.service.IPlaceEzdService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 易制毒场所Service业务层处理
 *
 * @author xiaohao
 * @date 2023-01-19
 */
@RequiredArgsConstructor
@Service
public class PlaceEzdServiceImpl implements IPlaceEzdService {

    private final PlaceEzdMapper baseMapper;
    private final TcCommonService tcCommonService;

    /**
     * 查询易制毒场所
     */
    @Override
    public PlaceEzdVo queryById(Long ezdId) {
        PlaceEzdVo result= baseMapper.selectVoById(ezdId);
        tcCommonService.renderTcInfo(Lists.newArrayList(result));
        return result;
    }

    /**
     * 查询易制毒场所列表
     */
    @Override
    public TableDataInfo<PlaceEzdVo> queryPageList(PlaceEzdBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PlaceEzd> lqw = buildQueryWrapper(bo);
        Page<PlaceEzdVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        tcCommonService.renderTcInfo(result.getRecords());
        return TableDataInfo.build(result);
    }

    /**
     * 查询易制毒场所列表
     */
    @Override
    public List<PlaceEzdVo> queryList(PlaceEzdBo bo) {
        LambdaQueryWrapper<PlaceEzd> lqw = buildQueryWrapper(bo);
        List<PlaceEzdVo> result = baseMapper.selectVoList(lqw);
        tcCommonService.renderTcInfo(result);
        return result;
    }

    private LambdaQueryWrapper<PlaceEzd> buildQueryWrapper(PlaceEzdBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PlaceEzd> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDwdq()), PlaceEzd::getDwdq, bo.getDwdq());
        lqw.eq(StringUtils.isNotBlank(bo.getZhzt()), PlaceEzd::getZhzt, bo.getZhzt());
        lqw.eq(StringUtils.isNotBlank(bo.getDjjdshzt()), PlaceEzd::getDjjdshzt, bo.getDjjdshzt());
        return lqw;
    }

    /**
     * 新增易制毒场所
     */
    @Override
    public Boolean insertByBo(PlaceEzdBo bo) {
        PlaceEzd add = BeanUtil.toBean(bo, PlaceEzd.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setEzdId(add.getEzdId());
        }
        return flag;
    }

    /**
     * 修改易制毒场所
     */
    @Override
    public Boolean updateByBo(PlaceEzdBo bo) {
        PlaceEzd update = BeanUtil.toBean(bo, PlaceEzd.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PlaceEzd entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除易制毒场所
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
